<template>
  <div class="count">
    <h2>当前求和为:{{ sum }}放大10倍后:{{ bigSum }}</h2>
    <h3>欢迎来到{{ school }},坐落于{{ upperSchool }}</h3>
    <select v-model.number="n">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    <button @click="add">加</button>
    <button @click="minus">减</button>
  </div>
</template>

<script setup lang="ts" name="Count">
import { ref, reactive } from "vue";
import { useCountStore } from "../store/count";
import { storeToRefs } from "pinia";

const countStore = useCountStore();
//以下两种方式都可以拿到state中的数据
//countStore.sum
//countStore.$state.sum
//reactive会帮你把里面的ref拆包，不需要.value

let n = ref(1);
//storeTorefs只会关注store中的数据
const { sum, school, address, bigSum, upperSchool } = storeToRefs(countStore);
function add() {
  //第一种修改方式
  // countStore.sum += n.value;
  // countStore.school = "空白";

  //第二种修改方式
  // countStore.$patch({
  //   sum: 888,
  //   school: "无",
  // });

  //第三种修改方式
  countStore.increment(n.value);
}
function minus() {
  countStore.sum -= n.value;
}
</script>

<style scoped>
.count {
  background-color: skyblue;
  padding: 10px;
  border-radius: 10px;
  box-shadow: 0 0 10px;
}
select,
button {
  margin: 0 5px;
  height: 25px;
}
</style>
